package com.kingsailor.gunsmith.admin.web.controller;

import edu.jetwinner.util.EasyStringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xulixin
 */
@Controller
public class DashboardController {

    private static final Logger logger = LoggerFactory.getLogger(DashboardController.class);

    @GetMapping(value = {"/", "/index", "/index.html"})
//    @GetMapping(value = { "${shiro.successUrl}","/"})
    public String index(ModelMap mmap) {
        return "index";
    }

    // 切换主题
    @GetMapping("/sys/switchSkin")
    public String skin(ModelMap mmap){
        return "skin";
    }

    // 显示默认首页
    @GetMapping("/main")
    public String showMainPage(ModelMap mmap){
        ClassPathResource resource = new ClassPathResource("/doc/changes.txt");
        if (!resource.exists()) {
            resource = new ClassPathResource("/changes.txt");
        }
        List<List<String>> entries = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(resource.getFile()))) {
            String s;
            List<String> entry = null;
            int lines = 0;
            while (lines < 8 && (s = br.readLine()) != null) {
                if (EasyStringUtil.isBlank(s)) {
                    lines++;
                    continue;
                }
                if (s.startsWith("v")) {
                    if (entry != null) {
                        entries.add(entry);
                    }
                    entry = new ArrayList<>();
                    entry.add(s);
                }
                if (entry != null && s.startsWith("*")) {
                    s = s.replace("*", " ");
                    entry.add(s.trim());
                }
            }
            if (entry != null) {
                entries.add(entry);
            }
        } catch (IOException ioe) {
            logger.error("ChangeLog Controller read file error.", ioe);
        }
        mmap.put("changeEntries", entries);
        return "main";
    }

    /**
     * 锁屏
     * @param mmap
     * @return
     * @throws Exception
     */
    @GetMapping("/Account/Lock")
    public String lock(ModelMap mmap) throws Exception {
        return "lock";
    }
}
